package com.empire.module.member.controller.admin.warnconfigpersonrelation;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.warnconfigpersonrelation.vo.*;
import com.empire.module.member.dal.dataobject.warnconfigpersonrelation.WarnConfigPersonRelationDO;
import com.empire.module.member.service.warnconfigpersonrelation.WarnConfigPersonRelationService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 预警条件-人员关系")
@RestController
@RequestMapping("/member/warn-config-person-relation")
@Validated
public class WarnConfigPersonRelationController {

    @Resource
    private WarnConfigPersonRelationService warnConfigPersonRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建预警条件-人员关系")
    @PreAuthorize("@ss.hasPermission('member:warn-config-person-relation:create')")
    public CommonResult<Integer> createWarnConfigPersonRelation(@Valid @RequestBody WarnConfigPersonRelationSaveReqVO createReqVO) {
        return success(warnConfigPersonRelationService.createWarnConfigPersonRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新预警条件-人员关系")
    @PreAuthorize("@ss.hasPermission('member:warn-config-person-relation:update')")
    public CommonResult<Boolean> updateWarnConfigPersonRelation(@Valid @RequestBody WarnConfigPersonRelationSaveReqVO updateReqVO) {
        warnConfigPersonRelationService.updateWarnConfigPersonRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预警条件-人员关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:warn-config-person-relation:delete')")
    public CommonResult<Boolean> deleteWarnConfigPersonRelation(@RequestParam("warnConfigId") String warnConfigId, @RequestParam("personId") Long personId) {
        warnConfigPersonRelationService.deleteWarnConfigPersonRelation(warnConfigId, personId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预警条件-人员关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:warn-config-person-relation:query')")
    public CommonResult<WarnConfigPersonRelationRespVO> getWarnConfigPersonRelation(@RequestParam("id") Long id) {
        WarnConfigPersonRelationDO warnConfigPersonRelation = warnConfigPersonRelationService.getWarnConfigPersonRelation(id);
        return success(BeanUtils.toBean(warnConfigPersonRelation, WarnConfigPersonRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得预警条件-人员关系分页")
    @PreAuthorize("@ss.hasPermission('member:warn-config-person-relation:query')")
    public CommonResult<PageResult<WarnConfigPersonRelationRespVO>> getWarnConfigPersonRelationPage(@Valid WarnConfigPersonRelationPageReqVO pageReqVO) {
        PageResult<WarnConfigPersonRelationDO> pageResult = warnConfigPersonRelationService.getWarnConfigPersonRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarnConfigPersonRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出预警条件-人员关系 Excel")
    @PreAuthorize("@ss.hasPermission('member:warn-config-person-relation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarnConfigPersonRelationExcel(@Valid WarnConfigPersonRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarnConfigPersonRelationDO> list = warnConfigPersonRelationService.getWarnConfigPersonRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "预警条件-人员关系.xls", "数据", WarnConfigPersonRelationRespVO.class,
                        BeanUtils.toBean(list, WarnConfigPersonRelationRespVO.class));
    }

}